package Utils 
{
	import flash.net.SharedObject;
	/**
	 * ...
	 * @author Danilo Gama
	 */
	public class Cookies 
	{
		private var cookiesLevelsPlayed:SharedObject;
		private var levelsPlayed:Array;
		private var pointsCollected:Array;
		private var pointsMax:Array;
		
		public function Cookies() 
		{
			cookiesLevelsPlayed = SharedObject.getLocal(Constants.playerName);
			
			levelsPlayed = new Array();
			pointsCollected = new Array();
			pointsMax = new Array();
			
			cookiesLevelsPlayed.clear();
		}
		
		public function ExistsCookies(namePlayer:String):Boolean
		{
			if (namePlayer == "")
			{
				namePlayer = "default";
			}
			cookiesLevelsPlayed = SharedObject.getLocal(namePlayer);
			
			if (cookiesLevelsPlayed.size <= 0)
			{
				return false;
			}
			
			return true;
		}
		
		public function Spawn(namePlayer:String) 
		{
			if (namePlayer == "")
				namePlayer = "default";
			cookiesLevelsPlayed = SharedObject.getLocal(namePlayer);
			cookiesLevelsPlayed.clear();
			levelsPlayed = new Array();
			pointsCollected = new Array();
			pointsMax = new Array();
		}
		
		public function SaveLevelCookies(numLevel:int):void
		{

			levelsPlayed.push(numLevel);
			
			cookiesLevelsPlayed.data.levelsPlayed = levelsPlayed;
			
			cookiesLevelsPlayed.flush();
		}
		
		public function GetMaxLevelPlayed():int
		{
			//trace(cookiesLevelsPlayed.size);
			//trace("numlevel "+cookiesLevelsPlayed.data.numLevel);
			if (cookiesLevelsPlayed.size <= 0)
				return 0;
				
			var max:int = 1;
			
			//trace("levels "+cookiesLevelsPlayed.data.levelsPlayed);
			for (var level:String in cookiesLevelsPlayed.data.levelsPlayed) 
			{
				//trace("level "+cookiesLevelsPlayed.data.levelsPlayed[level]);
				if (max < parseInt(cookiesLevelsPlayed.data.levelsPlayed[level]))
				{
					max = parseInt(cookiesLevelsPlayed.data.levelsPlayed[level]);
				}
			}
			//trace(max);
			return max;
		}
		
		public function GetMaxLevel():int {
			
			if (cookiesLevelsPlayed.size <= 0)
				return 0;
				
			var max:int = 0;
			for (var level:String in cookiesLevelsPlayed.data.levelsPlayed)
			{
				if (max < parseInt(cookiesLevelsPlayed.data.levelsPlayed[level]))
				{
					max = parseInt(cookiesLevelsPlayed.data.levelsPlayed[level]);
				}
			}
			return max;
		}
		
		public function IsMaxScore(numLevel:int):Boolean
		{
			var i:int = 0;
			for (var level:String in cookiesLevelsPlayed.data.levelsPlayed)
			{
				if (numLevel == parseInt(cookiesLevelsPlayed.data.levelsPlayed[level]))
				{
					trace("point "+cookiesLevelsPlayed.data.points[i]+" --- max "+cookiesLevelsPlayed.data.pointsMax[i]);
					if (cookiesLevelsPlayed.data.points[i] == cookiesLevelsPlayed.data.pointsMax[i])
					{
						return true;
					}
				}
				i++;
			}
			return false;
		}
	}
}